﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Configuration;
using Huayou.Common;
using PayRoller.Code;
using System.Web.SessionState;
using HySoft.Cash.Business;
using HySoft.Cash;
using HySoft.Cash.Structure;
using HiLand.Utility.Data;

namespace PayRoller.Manage.Reports
{
    public partial class CreditReportDetails : ParentPage, IRequiresSessionState
    {
        //int currentOrgID = 0;

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }

        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string commandText = "";

            string flowingID = this.Request.QueryString["flowingID"];
            if (string.IsNullOrEmpty(flowingID) == false)
            {
                commandText = string.Format("select r.*,c.memo as mainMemo,c.*,i.Sid as invoiceID from tCapitalFlowingRelating r left join tCapitalFlowing c on r.CapitalFlowingID= c.ID left join tInvoiceList i on r.RalatingID=i.Guid where r.CapitalFlowingID='{0}'", flowingID);
                //commandText = string.Format("select r.*,c.* from tCapitalFlowingRelating r left join tCapitalFlowing c on r.CapitalFlowingID= c.ID left join tInvoiceList i on r.RalatingID=i.guid where r.CapitalFlowingID='{0}'", flowingID);
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            }

            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }



        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        //protected override void Render(HtmlTextWriter writer)
        //{
        //    this.litDisplay.Text = string.Format("(Total Balance: {0})", this.totalBanlance.ToString("0.00"));
        //    base.Render(writer);
        //}


        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;

                HyperLink hlID = e.Item.FindControl("hlID") as HyperLink;
                if (hlID != null)
                { 
                    hlID.Text= drv["ID"].ToString();
                }

                HyperLink hlInvoiceID = e.Item.FindControl("hlInvoiceID") as HyperLink;
                if (hlInvoiceID != null)
                {
                    //hlInvoiceID.Text = drv["RalatingID"].ToString();
                    hlInvoiceID.Text = drv["invoiceID"].ToString();
                    hlInvoiceID.NavigateUrl = string.Format("~/manage/Invoices/InvoiceDetail.aspx?Guid={0}", drv["RalatingID"].ToString());
                }


                Label hlDate = e.Item.FindControl("hlDate") as Label;
                if (hlDate != null)
                {
                    hlDate.Text = Convert.ToDateTime(drv["TransDate"]).ToString(AppSettings.CurrentCulture);
                }


                Label hlPrincipal = e.Item.FindControl("hlPrincipal") as Label;
                if (hlPrincipal != null)
                {
                    hlPrincipal.Text = drv["Principal"].ToString();
                }

                Label hlInterest = e.Item.FindControl("hlInterest") as Label;
                if (hlInterest != null)
                {
                    hlInterest.Text = drv["Interest"].ToString();
                }

                Label hlPenalty = e.Item.FindControl("hlPenalty") as Label;
                if (hlPenalty != null)
                {
                    hlPenalty.Text = drv["Penalty"].ToString();
                }

                Label hlLateInterestCharges = e.Item.FindControl("hlLateInterestCharges") as Label;
                if (hlLateInterestCharges != null)
                {
                    hlLateInterestCharges.Text = drv["LateInterestCharges"].ToString();
                }

                Label hlStatus = e.Item.FindControl("hlStatus") as Label;
                if (hlStatus != null)
                {
                    int direction = Convert.ToInt32(drv["Direction"]);
                    hlStatus.Text = ((CapitalFlowingDirections)direction).ToString();
                }

                Label hlMemo = e.Item.FindControl("hlMemo") as Label;
                if (hlMemo != null)
                {
                    hlMemo.Text = drv["mainMemo"].ToString();
                }
            }
        }
    }
}